﻿using System;

namespace NumericalSolution {
  class GaussLagrange : NumericalIntegration {
    private int m_numberOfIntegrationPoints;
    private double[] m_integrationPoints;
    private double[] m_weights;

    public GaussLagrange(int numberOfIntegrationPoints) {

      m_numberOfIntegrationPoints = numberOfIntegrationPoints;

      m_integrationPoints = new double[m_numberOfIntegrationPoints];
      m_weights = new double[m_numberOfIntegrationPoints];

      if (m_numberOfIntegrationPoints == 1) {
        m_integrationPoints[0] = 0.0;
        m_weights[0] = 2.0;
        return;
      } else if (m_numberOfIntegrationPoints == 2) {
        m_integrationPoints[0] = -1 / Math.Sqrt(3);
        m_integrationPoints[1] = 1 / Math.Sqrt(3);
        m_weights[0] = 1.0;
        m_weights[1] = 1.0;
        return;
      } else if (m_numberOfIntegrationPoints == 3) {
        m_integrationPoints[0] = -Math.Sqrt(3 / 5);
        m_integrationPoints[1] = 0.0;
        m_integrationPoints[2] = Math.Sqrt(3 / 5);
        m_weights[0] = 5 / 9;
        m_weights[1] = 8 / 9;
        m_weights[2] = 5 / 9;
        return;
      }
      throw new ArgumentOutOfRangeException("Not yet implemented.");
    }

    public int NumberOfIntegrationPoints {
      get { return m_numberOfIntegrationPoints; }
      set { m_numberOfIntegrationPoints = value; }
    }

    public double Weight(int index) {
      return m_weights[index];
    }

    public double IntegrationPoint(int index) {
      return m_integrationPoints[index];
    }
  }
}
